package com.qiyi.player.core.model.remote
{
   import com.qiyi.player.base.rpc.impl.BaseRemoteObject;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.base.logging.ILogger;
   import flash.net.URLRequest;
   import flash.utils.getTimer;
   import com.qiyi.player.core.Config;
   import flash.net.URLLoaderDataFormat;
   import com.qiyi.player.core.model.utils.CachePool;
   import com.qiyi.player.core.player.def.PlayerUseTypeEnum;
   import com.qiyi.player.base.pub.ProcessesTimeRecord;
   import flash.utils.ByteArray;
   import flash.events.Event;
   import flash.utils.clearTimeout;
   import com.adobe.serialization.json.JSON;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import flash.net.sendToURL;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.model.utils.ErrorCodeUtils;
   import com.qiyi.player.base.logging.Log;
   
   public class RequestVRSRemote extends BaseRemoteObject
   {
      
      public function RequestVRSRemote(param1:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.model.remote.RequestVRSRemote");
         super(0,"RequestVRSRemote");
         this._holder = param1;
         this._holder.runtimeData.authenticationError = false;
         _timeout = Config.VRS_TIMEOUT;
         _retryMaxCount = Config.VRS_MAX_RETRY;
      }
      
      private var _holder:ICorePlayer;
      
      private var _requestDuration:int = 0;
      
      private var _log:ILogger;
      
      override protected function getRequest() : URLRequest {
         this._requestDuration = getTimer();
         if(this._holder.pingBack)
         {
            this._holder.pingBack.sendStartLoadVrs();
         }
         var _loc1_:Boolean = this._holder.runtimeData.movieIsMember;
         var _loc2_:* = "";
         if(_loc1_)
         {
            _loc2_ = Config.VRS_VIP_SERVER_URL;
         }
         else if(this._holder.runtimeData.tvid == "" || this._holder.runtimeData.tvid == "0" || this._holder.runtimeData.tvid == null || this._holder.runtimeData.tvid == "null")
         {
            _loc2_ = Config.VRS_SERVER_URL_VD;
         }
         else
         {
            _loc2_ = Config.VRS_SERVER_URL;
         }
         
         _loc2_ = _loc2_ + (this._holder.runtimeData.tvid + "/" + this._holder.runtimeData.originalVid);
         if(this._holder.runtimeData.vrsDomain != "")
         {
            _loc2_ = this._holder.runtimeData.vrsDomain + this._holder.runtimeData.tvid + "/" + this._holder.runtimeData.originalVid;
         }
         if(_loc1_)
         {
            _loc2_ = _loc2_ + ("/" + this._holder.runtimeData.communicationlId + "/" + this._holder.runtimeData.key + "/" + this._holder.runtimeData.QY00001);
            _loader.dataFormat = URLLoaderDataFormat.BINARY;
         }
         _loc2_ = _loc2_ + "/";
         return new URLRequest(_loc2_);
      }
      
      override public function initialize() : void {
         var _loc1_:String = CachePool.getVRSSource(this._holder.runtimeData.tvid,this._holder.runtimeData.originalVid);
         if((this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.PREVIEW || this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.SHARE_SECTION) && !this._holder.runtimeData.movieIsMember && (_loc1_))
         {
            this.onComplete(null);
         }
         else
         {
            ProcessesTimeRecord.STime_VP = getTimer();
            super.initialize();
         }
      }
      
      private function rotate_right(param1:uint, param2:int) : uint {
         var _loc3_:uint = 0;
         var _loc4_:* = 0;
         while(_loc4_ < param2)
         {
            _loc3_ = param1 & 1;
            param1 = param1 >>> 1;
            _loc3_ = _loc3_ << 31;
            param1 = param1 + _loc3_;
            _loc4_++;
         }
         return param1;
      }
      
      private function decode(param1:ByteArray) : String {
         var _loc8_:uint = 0;
         var _loc2_:ByteArray = new ByteArray();
         var _loc3_:int = param1.length;
         param1.position = 0;
         var _loc4_:* = 0;
         var _loc5_:* = 20110218;
         var _loc6_:int = _loc5_ % 100;
         var _loc7_:int = _loc3_ % 4;
         _loc4_ = 0;
         while(_loc4_ + 4 <= _loc3_)
         {
            _loc8_ = uint(param1.readUnsignedByte() << 24 | param1.readUnsignedByte() << 16 | param1.readUnsignedByte() << 8 | param1.readUnsignedByte());
            _loc8_ = _loc8_ ^ _loc5_;
            _loc8_ = this.rotate_right(_loc8_,_loc6_);
            _loc2_.writeByte((_loc8_ & 4.27819008E9) >> 24);
            _loc2_.writeByte((_loc8_ & 16711680) >> 16);
            _loc2_.writeByte((_loc8_ & 65280) >> 8);
            _loc2_.writeByte(_loc8_ & 255);
            _loc4_ = _loc4_ + 4;
         }
         _loc4_ = 0;
         while(_loc4_ < _loc7_)
         {
            _loc2_.writeByte(param1.readUnsignedByte());
            _loc4_++;
         }
         _loc2_.position = 0;
         return _loc2_.readMultiByte(_loc2_.bytesAvailable,"utf-8");
      }
      
      override protected function onComplete(param1:Event) : void {
         var jsonStr:String = null;
         var status:int = 0;
         var event:Event = param1;
         if(this._requestDuration > 0)
         {
            this._requestDuration = getTimer() - this._requestDuration;
            if(this._holder.pingBack)
            {
               this._holder.pingBack.sendVRSRequestTime(this._requestDuration);
            }
         }
         clearTimeout(_waitingResponse);
         _waitingResponse = 0;
         jsonStr = "";
         var json:Object = null;
         if(this._holder.runtimeData.movieIsMember)
         {
            ProcessesTimeRecord.usedTime_VP = getTimer() - ProcessesTimeRecord.STime_VP;
            this._log.debug("the movie is member, prepare to decode...");
            try
            {
               jsonStr = this.decode(_loader.data as ByteArray);
               json = com.adobe.serialization.json.JSON.decode(com.adobe.serialization.json.JSON.decode(jsonStr).data.video);
               jsonStr = com.adobe.serialization.json.JSON.encode(json);
               _data = jsonStr;
            }
            catch(e:Error)
            {
               _log.fatal("RequestVRSRemote: failed to transform:" + e.errorID);
               if(jsonStr)
               {
                  _log.info(jsonStr.substr(0,100));
                  sendHijackPingBack(jsonStr);
               }
               this.setStatus(RemoteObjectStatusEnum.DataError);
               return;
            }
         }
         else
         {
            try
            {
               if(event)
               {
                  ProcessesTimeRecord.usedTime_VP = getTimer() - ProcessesTimeRecord.STime_VP;
                  jsonStr = _loader.data;
               }
               else
               {
                  this._log.debug("use cache pool vrs source,tvid:" + this._holder.runtimeData.tvid + ",vid:" + this._holder.runtimeData.originalVid);
                  jsonStr = CachePool.getVRSSource(this._holder.runtimeData.tvid,this._holder.runtimeData.originalVid);
               }
               json = com.adobe.serialization.json.JSON.decode(jsonStr);
               _data = jsonStr;
            }
            catch(e:Error)
            {
               _log.fatal("the vrs data is invalid:");
               if(jsonStr)
               {
                  _log.info(jsonStr.substr(0,100));
                  sendHijackPingBack(jsonStr);
               }
               this.setStatus(RemoteObjectStatusEnum.DataError);
               return;
            }
         }
         if(this._holder.runtimeData.movieIsMember)
         {
            try
            {
               status = int(json.st);
               if(status >= 200 || status <= 100)
               {
                  this._holder.runtimeData.errorCodeValue = json;
                  this._log.info("vrs status:" + status);
                  this.setStatus(RemoteObjectStatusEnum.AuthenticationError);
               }
               else
               {
                  super.onComplete(event);
               }
            }
            catch(e:Error)
            {
               _log.fatal("the vrs data is invalid:");
               if(jsonStr)
               {
                  _log.info(jsonStr.substr(0,100));
               }
               setStatus(RemoteObjectStatusEnum.DataError);
            }
            return;
         }
         status = int(json.st);
         if(status >= 200 || status <= 100)
         {
            this._holder.runtimeData.errorCodeValue = json;
            this._log.info("vrs status:" + status);
            this.setStatus(RemoteObjectStatusEnum.AuthenticationError);
         }
         else
         {
            super.onComplete(event);
         }
      }
      
      private function sendHijackPingBack(param1:String) : void {
         var request:URLRequest = null;
         var var_30:String = param1;
         if(var_30)
         {
            try
            {
               request = new URLRequest();
               request.url = "http://msg.video.qiyi.com/tmpstats.gif?type=isphijack20140210&rt=" + encodeURIComponent(var_30.substr(0,500)) + "&tn=" + Math.random();
               sendToURL(request);
            }
            catch(e:Error)
            {
            }
         }
      }
      
      override protected function setStatus(param1:EnumItem) : void {
         var _loc2_:* = 0;
         if(param1 == RemoteObjectStatusEnum.Timeout || param1 == RemoteObjectStatusEnum.ConnectError || param1 == RemoteObjectStatusEnum.DataError || param1 == RemoteObjectStatusEnum.AuthenticationError || param1 == RemoteObjectStatusEnum.SecurityError || param1 == RemoteObjectStatusEnum.UnknownError)
         {
            _loc2_ = ErrorCodeUtils.getErrorCodeByRemoteObject(this,param1);
            if(this._holder.pingBack)
            {
               this._holder.pingBack.sendError(_loc2_);
            }
            this._holder.runtimeData.errorCode = _loc2_;
         }
         super.setStatus(param1);
      }
   }
}
